// pages/coupon/coupon.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    couponList:[],
    userInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;
    that.getCoupon();
  },
  //获取优惠卷列表
  getCoupon:function(){

    var that = this;

    var userInfo = util.getKey("userInfo");
    httpUtil.getHttp("/coupon/get", { userId: userInfo.id }, function (res) {
      that.setData({
        couponList: res,
        userInfo: userInfo
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  swichNav: function(e) {
    var that = this;
    if (this.data.currentTab === e.currentTarget.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current
      })
    }
  },
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  //使用优惠卷
  clickReceive: function (event){
    var that=this;
    var userInfo = that.data.userInfo;
    var id = event.target.dataset.id;
    httpUtil.getHttp("/coupon/set", { userId: userInfo.id, id: id }, function (res) {
      wx.showToast({
        title: '领取成功',
        icon: 'succes',
        duration: 1000,
        mask: true
      })
      that.getCoupon();
       that.setData({
            currentTab: 0
        })
    })
  },
  //查找商品
  addGoods: function (e) {
     var id = e.target.dataset.id;
    var goodsType = e.target.dataset.type;

    if (id == undefined) {
      return;
    }

    if (goodsType == undefined) {
      return;
    }

    if (goodsType != undefined && goodsType == 4) {
      wx.navigateTo({
        url: '../detail/detail?id=' + id
      })
      return
    }
    if (goodsType != undefined && goodsType == 0) {
      wx.navigateTo({
        url: '../visaInfo/visaInfo?id=' + id
      })
      return
    }
    wx: wx.navigateTo({
      url: '../proInfo/proInfo?id=' + id,
    })
      wx.showToast({
        title: '跳转中。。',
        icon: 'succes',
        duration: 1000,
        mask: true
      })
      that.getCoupon();

 
  }
})